<?php
namespace Admin\Model;
use Think\Model;
class NetModel extends Model 
{
	protected $insertFields = array('name','sernumber','project_name','status','con_time','reg_time');
	protected $updateFields = array('id','name','sernumber','project_name','status','con_time','reg_time');
	protected $_validate = array(
		array('name', '1,255', '的值最长不能超过 255 个字符！', 2, 'length', 3),
		array('sernumber', '1,255', '的值最长不能超过 255 个字符！', 2, 'length', 3),
		array('project_name', '1,255', '的值最长不能超过 255 个字符！', 2, 'length', 3),
		array('status', 'number', '必须是一个整数！', 2, 'regex', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($name = I('get.name'))
			$where['name'] = array('like', "%$name%");
		if($sernumber = I('get.sernumber'))
			$where['sernumber'] = array('like', "%$sernumber%");
		if($project_name = I('get.project_name'))
			$where['project_name'] = array('like', "%$project_name%");
		if($status = I('get.status'))
			$where['status'] = array('eq', $status);
		$con_timefrom = I('get.con_timefrom');
		$con_timeto = I('get.con_timeto');
		if($con_timefrom && $con_timeto)
			$where['con_time'] = array('between', array(strtotime("$con_timefrom 00:00:00"), strtotime("$con_timeto 23:59:59")));
		elseif($con_timefrom)
			$where['con_time'] = array('egt', strtotime("$con_timefrom 00:00:00"));
		elseif($con_timeto)
			$where['con_time'] = array('elt', strtotime("$con_timeto 23:59:59"));
		$reg_timefrom = I('get.reg_timefrom');
		$reg_timeto = I('get.reg_timeto');
		if($reg_timefrom && $reg_timeto)
			$where['reg_time'] = array('between', array(strtotime("$reg_timefrom 00:00:00"), strtotime("$reg_timeto 23:59:59")));
		elseif($reg_timefrom)
			$where['reg_time'] = array('egt', strtotime("$reg_timefrom 00:00:00"));
		elseif($reg_timeto)
			$where['reg_time'] = array('elt', strtotime("$reg_timeto 23:59:59"));
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/
}